///方法集合
use std::collections::HashMap;
use super::room::GameRoom;
use super::PlayerStage;
use super::message::*;

use chess::ChessResult;

///落子操作。
/// 落子失败：返回错误信息；
/// 落子成功：检查成三，若成三，则返回成三信息；未成三，返回落子信息。
pub fn put_piece(player: u32, location: u8, room: &mut GameRoom)  -> Vec<PlayerResponse>{
    let mut vec = Vec::<PlayerResponse>::with_capacity(2);
    let result = room.put(player, location);
    match result {
        ChessResult::Ok => {
            let three = room.check_three(location);
            if 0 < three{
                if room.current_can_kill(){
                    room.set_kill(three);
                    let board = room.point_array();
                    let current_message = ChessResponse::new(board, false, String::from("kill"), Operation::Put(location));
                    let board = room.point_array();
                    let another_message = ChessResponse::new(board, true, String::from("kill"), Operation::Put(location));
                    vec.push(PlayerResponse::ChessOk(player, current_message));
                    vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
                }else {
                    room.player_stage();
                    room.correct_stage();
                    room.player_rotation();
                    let board = room.point_array();
                    let current_message = ChessResponse::new(board, false, room.next_operate(), Operation::Put(location));
                    let board = room.point_array();
                    let another_message = ChessResponse::new(board, true, room.next_operate(), Operation::Put(location));
                    vec.push(PlayerResponse::ChessOk(player, current_message));
                    vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
                }
            }else {
                room.player_stage();
                room.correct_stage();
                room.player_rotation();
                let board = room.point_array();
                let current_message = ChessResponse::new(board, false, room.next_operate(), Operation::Put(location));
                let board = room.point_array();
                let another_message = ChessResponse::new(board, true, room.next_operate(), Operation::Put(location));
                vec.push(PlayerResponse::ChessOk(player, current_message));
                vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
            }
        },
        ChessResult::Error(code, message) => {
            vec.push(PlayerResponse::Error(player, message));
        }
    };
    vec
}

///吃子。吃子成功，首先检查是否赢棋。
pub fn kill_piece(player: u32, location: u8, room: &mut GameRoom)  -> Vec<PlayerResponse>{
    let mut vec = Vec::<PlayerResponse>::with_capacity(2);
    let result = room.kill(player, location);
    match result {
        ChessResult::Ok => {
            room.kill_ok();
            if room.is_current_win(){
                vec.push(PlayerResponse::GameOver(player, "winner".to_string()));
                vec.push(PlayerResponse::GameOver(room.opponent_id(), "loser".to_string()));
                return vec;
            }
            let times = room.kill_times();
            if times > 0{
                if room.current_can_kill(){
                    let board = room.point_array();
                    let current_message = ChessResponse::new(board, false, String::from("kill"), Operation::Put(location));
                    let board = room.point_array();
                    let another_message = ChessResponse::new(board, true, String::from("kill"), Operation::Put(location));
                    vec.push(PlayerResponse::ChessOk(player, current_message));
                    vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
                    return vec;
                }
            }
            room.player_rotation();
            let board = room.point_array();
            let current_message = ChessResponse::new(board, false, room.next_operate(), Operation::Kill(location));
            let board = room.point_array();
            let another_message = ChessResponse::new(board, true, room.next_operate(), Operation::Kill(location));
            vec.push(PlayerResponse::ChessOk(player, current_message));
            vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
        },
        ChessResult::Error(code, message) => {
            vec.push(PlayerResponse::Error(player, message));
        }
    };
    vec
}

///走棋成功后，要做几个处理：
/// 1.查看是否成三，成三则走吃子流程；
pub fn move_piece(player: u32, source: u8, target: u8, room: &mut GameRoom)  -> Vec<PlayerResponse>{
    let mut vec = Vec::<PlayerResponse>::with_capacity(2);
    let result = room.pmove(player, source, target);
    match result {
        ChessResult::Ok => {
            let three = room.check_three(target);
            if 0 < three{
                if room.current_can_kill(){
                    room.set_kill(three);
                    let board = room.point_array();
                    let current_message = ChessResponse::new(board, false, String::from("kill"), Operation::Move(source, target));
                    let board = room.point_array();
                    let another_message = ChessResponse::new(board, true, String::from("kill"), Operation::Move(source, target));
                    vec.push(PlayerResponse::ChessOk(player, current_message));
                    vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
                }else {
                    room.player_rotation();
                    let board = room.point_array();
                    let current_message = ChessResponse::new(board, false, "move".to_string(), Operation::Move(source, target));
                    let board = room.point_array();
                    let another_message = ChessResponse::new(board, true, "move".to_string(), Operation::Move(source, target));
                    vec.push(PlayerResponse::ChessOk(player, current_message));
                    vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
                }
            }else {
                room.player_rotation();
                let board = room.point_array();
                let current_message = ChessResponse::new(board, false, "move".to_string(), Operation::Move(source, target));
                let board = room.point_array();
                let another_message = ChessResponse::new(board, true, "move".to_string(), Operation::Move(source, target));
                vec.push(PlayerResponse::ChessOk(player, current_message));
                vec.push(PlayerResponse::ChessOk(room.opponent_id(), another_message));
            }
        },
        ChessResult::Error(code, message) => {
            vec.push(PlayerResponse::Error(player, message));
        }
    };
    vec
}